/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package filecopier.gui;

import java.awt.event.ActionEvent;
import java.net.URL;
import java.io.IOException;
import javax.swing.JDialog;
import javax.swing.JPanel;
import java.util.Deque;
import java.util.ArrayDeque;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.Box;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.Document;

import java.awt.BorderLayout;
import java.awt.Dimension;
import filecopier.Singleton;

/**
 *
 * @author david
 */
public class HelpDialog extends JDialog {
    
    private JPanel contentPane = null;
    private JEditorPane textPane = null;
    private Deque<URL> backDeque = null;
    private Deque<URL> forwardDeque = null;
    private JButton backBtn = null;
    private JButton forwardBtn = null;
    private JButton closeBtn = null;
    
    //TODO: Add back and forward buttons from the images directory.
    
    public HelpDialog() {
        super(Singleton.getInstance().getFrame());
        initialize();
    }
    
    private void initialize() {
        backDeque = new ArrayDeque<URL>();
        forwardDeque = new ArrayDeque<URL>();
        setTitle("File Copier Help");
        setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        setContentPane(getJContentPane());
        
        this.addWindowListener(new java.awt.event.WindowAdapter()  {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                setVisible(false);
            }
        });
        
        addComponentListener(new java.awt.event.ComponentAdapter() {
            
            @Override
            public void componentShown(java.awt.event.ComponentEvent e) {
                Dimension preferredSize = getPreferredSize();
                Dimension actualSize = getSize();
                
                if(actualSize.height < preferredSize.height || actualSize.width < preferredSize.width) {
                    setSize(preferredSize);
                }
            }
        });
        
        pack();
        setVisible(true);
    }
    
    private JPanel getJContentPane()
    {
        if(contentPane == null)
        {
            contentPane = new JPanel();
            contentPane.setLayout(new BorderLayout());
            contentPane.setOpaque(true);
        
            contentPane.add(createBtnBox(), BorderLayout.NORTH);
            contentPane.add(createScrollPane(), BorderLayout.CENTER);
            contentPane.add(createCloseBtn(), BorderLayout.SOUTH);
        }
        return contentPane;
    }
    
    private JScrollPane createScrollPane() {
        JScrollPane editorScrollPane  = new JScrollPane(createTextPane());
        editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        editorScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        editorScrollPane.setPreferredSize(new java.awt.Dimension(600,500));
        
        return editorScrollPane;
    }
    
    private JEditorPane createTextPane() {
        textPane = new JEditorPane();
        textPane.setEditable(false);

        textPane.addHyperlinkListener(new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent e) {
                if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                    Document doc = textPane.getDocument();
                    try {
                        URL url = e.getURL();
                        URL currentUrl = textPane.getPage();
                        doc.putProperty(Document.StreamDescriptionProperty, null);
                        textPane.setPage(url);
                        backDeque.push(currentUrl);
                        if(!backBtn.isEnabled()) {
                            backBtn.setEnabled(true);
                        }
                        if(!forwardDeque.isEmpty()) {
                            forwardDeque.clear();
                            forwardBtn.setEnabled(false);
                        }
                    }
                    catch(IOException ioe) {
                        javax.swing.JOptionPane.showMessageDialog(null, "Can't follow link","Invalid Input",
                                javax.swing.JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        
        java.net.URL helpURL = HelpDialog.class.getResource("helpHtml/Index.html");
        if (helpURL != null) {
            try {
                textPane.setPage(helpURL);
            } catch (IOException e) {
                System.err.println("Attempted to read a bad URL: " + helpURL);
            }
        } else {
            System.err.println("Couldn't find file: FileCopierHelp.html");
        }
        
        return textPane;
    }
    
    private JButton createBackBtn() {
        if(backBtn == null) {
            backBtn = new JButton();
            backBtn.setMnemonic(java.awt.event.KeyEvent.VK_B);
            ImageIcon backImage = createImageIcon("images/go-previous22.png", "back");
            if(backImage != null) backBtn.setIcon(backImage);
            else backBtn.setText("Back");
            
            backBtn.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    URL currentURL = textPane.getPage();
                    URL backURL = backDeque.pop();
                    if(backURL != null) {
                        try {
                            Document doc = textPane.getDocument();
                            doc.putProperty(Document.StreamDescriptionProperty, null);
                            textPane.setPage(backURL);
                            forwardDeque.push(currentURL);
                            if(!forwardBtn.isEnabled()) forwardBtn.setEnabled(true);
                            if(backDeque.isEmpty()) backBtn.setEnabled(false);
                        } catch(IOException ex) {
                            javax.swing.JOptionPane.showMessageDialog(null, "Can't follow link","Invalid Input",
                                    javax.swing.JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            });
            backBtn.setEnabled(false);
        }
        return backBtn;
    }
    
    private JButton createForwardBtn() {
        if(forwardBtn == null) {
            forwardBtn = new JButton();
            forwardBtn.setMnemonic(java.awt.event.KeyEvent.VK_F);
            ImageIcon fwdImage = createImageIcon("images/go-next22.png", "forward");
            if(fwdImage != null) forwardBtn.setIcon(fwdImage);
            else forwardBtn.setText("Forward");
            forwardBtn.setEnabled(false);
            
            forwardBtn.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    URL currentURL = textPane.getPage();
                    URL fwdURL = forwardDeque.pop();
                    if(fwdURL != null) {
                        try {
                            Document doc = textPane.getDocument();
                            doc.putProperty(Document.StreamDescriptionProperty, null);
                            textPane.setPage(fwdURL);
                            backDeque.push(currentURL);
                            if(!backBtn.isEnabled()) backBtn.setEnabled(true);
                            if(forwardDeque.isEmpty()) forwardBtn.setEnabled(false);
                        } catch(IOException ex) {
                            javax.swing.JOptionPane.showMessageDialog(null, "Can't follow link","Invalid Input",
                                    javax.swing.JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            });
        }
        return forwardBtn;
    }
    
    private Box createBtnBox() {
        Box btnBox = Box.createHorizontalBox();
        btnBox.add(createBackBtn());
        btnBox.add(createForwardBtn());
        return btnBox;
    }
    
    private JButton createCloseBtn() {
        if(closeBtn == null) {
            closeBtn = new JButton("Close");
            closeBtn.setMnemonic(java.awt.event.KeyEvent.VK_C);
            closeBtn.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    HelpDialog.this.setVisible(false);
                }
            });
        }
        return closeBtn;
    }
    
    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(final String path, final String description) {
        java.net.URL imgURL = HelpDialog.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL, description);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
    
}
